#region File Description
//-----------------------------------------------------------------------------
// Projectile.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using Microsoft.Xna.Framework;
using PloobsEngine.Utils;
#endregion

namespace PloobsEngine.Particle3D
{
    /// <summary>
    /// This class demonstrates how to combine several different particle systems
    /// to build up a more sophisticated composite effect. It implements a rocket
    /// projectile, which arcs up into the sky using a ParticleEmitter to leave a
    /// steady stream of trail particles behind it. After a while it explodes,
    /// creating a sudden burst of explosion and smoke particles.
    /// </summary>
    public class ProjectileEmiter : GeneralEmiter
    {
        #region Constants

        const float trailParticlesPerSecond = 200;
        const int numExplosionParticles = 30;
        const int numExplosionSmokeParticles = 50;
        const float projectileLifespan = 1.5f;
        const float sidewaysVelocityRange = 60;
        const float verticalVelocityRange = 40;
        const float gravity = 15;

        #endregion

        #region Fields

        Vector3 position;
        Vector3 velocity;
        float age;
        FollowPointEmiter trailEmitter;       

        #endregion


        /// <summary>
        /// Constructs a new projectile.
        /// </summary>
        public ProjectileEmiter(Vector3 position)
        {
           this.position = position;                     
            // Start at the origin, firing in a random (but roughly upward) direction.            

           velocity.X = (float)(StaticRandom.Random() - 0.5) * sidewaysVelocityRange;
           velocity.Y = (float)(StaticRandom.Random() + 0.5) * verticalVelocityRange;
           velocity.Z = (float)(StaticRandom.Random() - 0.5) * sidewaysVelocityRange;
        
            // Use the particle emitter helper to output our trail particles.
            trailEmitter = new FollowPointEmiter("ProjectileTrailParticleSystem",
                                               trailParticlesPerSecond, position);
        }

        public ProjectileEmiter(Vector3 position,Vector3 speed)
        {
            this.position = position;
            // Start at the origin, firing in a random (but roughly upward) direction.            

            velocity = speed;

            // Use the particle emitter helper to output our trail particles.
            trailEmitter = new FollowPointEmiter("ProjectileTrailParticleSystem",
                                               trailParticlesPerSecond, position);
        }


        public override void Initialize()
        {
            particleManager.AddEmiter(trailEmitter);
        }

        public override string[] ParticleSystemUsed()
        {
            return new string[] { "ProjectileTrailParticleSystem", "ExplosionSmokeParticleSystem", "ExplosionParticleSystem" };
        }


        public override void UpdateEmiter(GameTime gt)
        {
            if (!UpdateE(gt))
            {
                this.Remove();
                trailEmitter.Remove();
            }
        }

        /// <summary>
        /// Updates the projectile.
        /// </summary>
        public bool UpdateE(GameTime gameTime)
        {
            float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

            // Simple projectile physics.
            position += velocity * elapsedTime;
            velocity.Y -= elapsedTime * gravity;
            age += elapsedTime;

            // Update the particle emitter, which will create our particle trail.
            trailEmitter.NewPosition =  position;

            // If enough time has passed, explode! Note how we pass our velocity
            // in to the AddParticle method: this lets the explosion be influenced
            // by the speed and direction of the projectile which created it.
            if (age > projectileLifespan)
            {
                for (int i = 0; i < numExplosionParticles; i++)
                    this.ParticlesSystems["ExplosionParticleSystem"].AddParticle(position, velocity);

                for (int i = 0; i < numExplosionSmokeParticles; i++)
                    this.ParticlesSystems["ExplosionSmokeParticleSystem"].AddParticle(position, velocity);

                return false;
            }
                
            return true;
        }
    }
}
